package com.happy_hao.topbiz.service.impl;

import com.happy_hao.topbiz.api.UserApi;
import com.happy_hao.topbiz.common.Result;
import com.happy_hao.topbiz.dto.EmailCodeRequest;
import com.happy_hao.topbiz.dto.FormRequest;
import com.happy_hao.topbiz.dto.userDto.CreateUserRequest;
import com.happy_hao.topbiz.service.Oauth2Service;
import com.happy_hao.topbiz.service.RegisterService;
import jakarta.annotation.Resource;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.springframework.stereotype.Service;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Resource
    private UserApi userApi;

    @Resource
    private Oauth2Service oauth2Service;

    @Override
    public Result form(FormRequest formRequest) {
        CreateUserRequest createUserRequest = new CreateUserRequest();
        createUserRequest.setIdentifier(formRequest.getIdentifier());
        DefaultPasswordService defaultPasswordService = new DefaultPasswordService();
        createUserRequest.setEncryptedPassword(defaultPasswordService.encryptPassword(formRequest.getPassword()));
        return userApi.createUser(createUserRequest);
    }

    @Override
    public Result code(EmailCodeRequest emailCodeRequest) {
        CreateUserRequest createUserRequest = new CreateUserRequest();
        createUserRequest.setIdentifier(emailCodeRequest.getEmail());
        return userApi.createUser(createUserRequest);
    }

    @Override
    public Result feishu(String code) {
        CreateUserRequest createUserRequest = new CreateUserRequest();
        createUserRequest.setIdentifier(oauth2Service.getPhoneByCode(code, false));
        return userApi.createUser(createUserRequest);
    }
}
